package com.kedou.app;

import com.kedou.domain.User;
import com.kedou.net.PostFollowProject;
import com.kedou.net.PostUserInfo;
import com.kedou.util.NetworkUtil;
import com.kedou.util.PreferencesUtil;
import com.kedou.util.Util;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 我的蝌蚪 界面
 * 
 * @author zhouchao
 * 
 */
public class Myactivity extends BaseActivity implements OnClickListener {
	/**
	 * 中间 Title
	 */
	private TextView mTvTitle;
	/**
	 * 右侧 设置
	 */
	private ImageView mImgSetting;
	/**
	 * 我的头像等信息 RelativeLayout
	 */
	private RelativeLayout mRlMyinformation;
	/**
	 * 我的财富 RelativeLayout
	 */
	private RelativeLayout mRlMyMoney;
	/**
	 * 我的蝌蚪 RelativeLayout
	 */
	private RelativeLayout mRlMyMount;
	/**
	 * 我的粉丝 RelativeLayout
	 */
	private RelativeLayout mRlMyFans;
	/**
	 * 头像
	 */
	private ImageView mImgMyHead;

	/**
	 * 名字
	 */
	private TextView mTvMyName;
	/**
	 * 邮箱
	 */
	private TextView mTvMyEmail;

	/**
	 * 是否认证
	 */
	private TextView mTvWhetherCertification;

	/**
	 * 是否认证
	 */
	private ImageView mImgWhetherCertification;

	/**
	 * 我的财富
	 */
	private TextView mTvMyMoney;

	/**
	 * 我的蝌蚪
	 */
	private TextView mTvMyMount;

	/**
	 * 我的粉丝
	 */
	private TextView mTvMyFans;
	private User user;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_my);
		initView();
		setListener();
		getDataFromServer();
	}

	/**
	 * 从后台获取数据
	 */
	private void getDataFromServer() {
		mProgressDialog.show();
		if (NetworkUtil.isNetworkAvailable(Myactivity.this)) {
			new PostUserInfo(PreferencesUtil.getString(Myactivity.this,
					"token"), PreferencesUtil.getString(Myactivity.this,
					"userId"), Config.SOURCE, Util.getImei(Myactivity.this),
					new PostUserInfo.SuccessCallback() {

						@Override
						public void onSuccess(String msg, User mUser) {
							user = mUser;
							mProgressDialog.dismiss();
							// TODO 头像信息
							mTvMyName.setText(mUser.getRealName());
							mTvMyEmail.setText(mUser.getEmail());
							initCertification(mUser.getCertStatus());
							mTvMyMoney.setText(mUser.getMyWealth());
							mTvMyMount.setText(String.valueOf(mUser
									.getKedouNum()));
							mTvMyFans.setText(String.valueOf(mUser.getFansNum()));
						}
					}, new PostUserInfo.FailCallback() {

						@Override
						public void onFail(String msg) {
							mProgressDialog.dismiss();
							Toast.makeText(Myactivity.this, msg,
									Toast.LENGTH_SHORT).show();
						}
					}, Myactivity.this);
		} else {
			Toast.makeText(this,
					getResources().getString(R.string.check_your_network),
					Toast.LENGTH_SHORT).show();
		}
	}

	/**
	 * 初始化认证状态
	 */
	protected void initCertification(int status) {
		switch (status) {
		// 已认证
		case 0:
			mTvWhetherCertification.setText(getResources().getString(
					R.string.has_certified));
			mImgWhetherCertification
					.setBackgroundResource(R.drawable.downlistretry);
			break;
		// 未认证
		case 1:
			mTvWhetherCertification.setText(getResources().getString(
					R.string.no_certified));
			mImgWhetherCertification.setBackgroundResource(R.drawable.no_cer);
			break;
		}
	}

	/**
	 * 初始化View
	 */
	private void initView() {
		mTvTitle = (TextView) findViewById(R.id.title);
		mTvTitle.setText(R.string.title_my);
		mImgSetting = (ImageView) findViewById(R.id.img_setting);
		mImgSetting.setVisibility(View.VISIBLE);
		mRlMyinformation = (RelativeLayout) findViewById(R.id.rl_my_information);
		mRlMyMoney = (RelativeLayout) findViewById(R.id.rl_my_money);
		mRlMyMount = (RelativeLayout) findViewById(R.id.rl_my_mount);
		mRlMyFans = (RelativeLayout) findViewById(R.id.rl_my_fans);
		mImgMyHead = (ImageView) findViewById(R.id.img_my_head);
		mTvMyName = (TextView) findViewById(R.id.tv_my_name);
		mTvMyEmail = (TextView) findViewById(R.id.tv_my_email);
		mTvWhetherCertification = (TextView) findViewById(R.id.tv_whether_certification);
		mImgWhetherCertification = (ImageView) findViewById(R.id.img_whether_certification);
		mTvMyMoney = (TextView) findViewById(R.id.tv_my_money);
		mTvMyMount = (TextView) findViewById(R.id.tv_my_mount);
		mTvMyFans = (TextView) findViewById(R.id.tv_my_fans);
	}

	/**
	 * 设置监听器
	 */
	private void setListener() {
		mRlMyinformation.setOnClickListener(this);
		mRlMyMoney.setOnClickListener(this);
		mRlMyMount.setOnClickListener(this);
	}

	/**
	 * 点击事件
	 */
	@Override
	public void onClick(View v) {
		Intent intent;
		switch (v.getId()) {
		case R.id.rl_my_information:
			intent = new Intent(this, ModifyMyInformationActivity.class);
			Bundle bundle = new Bundle();
			bundle.putParcelable("user", user);
			intent.putExtra("user", bundle);
			this.startActivity(intent);
			break;
		case R.id.rl_my_money:
			intent = new Intent(this, MyMoneyActivity.class);
			this.startActivity(intent);
			break;
		case R.id.rl_my_mount:
			intent = new Intent(this, MymountActivity.class);
			this.startActivity(intent);
			break;
		}
	}
}
